package com.getpebble.android.bluetooth.receiver;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build.VERSION;
import com.getpebble.android.bluetooth.device.RemoteDeviceMetaData;
import com.getpebble.android.common.core.trace.Trace;
import java.lang.ref.WeakReference;

public abstract class DiscoveryReceiver extends BroadcastReceiver {
    private static final String TAG = DiscoveryReceiver.class.getSimpleName();
    private WeakReference<Context> mContext = null;

    public abstract void onDeviceFound(RemoteDeviceMetaData remoteDeviceMetaData);

    public abstract void onDiscoveryFinished();

    public abstract void onDiscoveryStarted();

    public DiscoveryReceiver(Context context) throws IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.bluetooth.adapter.action.DISCOVERY_STARTED");
        intentFilter.addAction("android.bluetooth.adapter.action.DISCOVERY_FINISHED");
        intentFilter.addAction("android.bluetooth.device.action.FOUND");
        Trace.verbose(TAG, "Registering discovery receiver");
        context.registerReceiver(this, intentFilter);
        this.mContext = new WeakReference(context);
    }

    @SuppressLint({"NewApi"})
    public final void onReceive(Context context, Intent intent) {
        Trace.debug(TAG, "onReceive(" + context + ", " + intent + ")");
        if (intent != null) {
            String action = intent.getAction();
            if (action != null) {
                if (action.equals("android.bluetooth.adapter.action.DISCOVERY_STARTED")) {
                    Trace.debug(TAG, "Discovery started.");
                    onDiscoveryStarted();
                } else if (action.equals("android.bluetooth.device.action.FOUND")) {
                    BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    if (device != null) {
                        Trace.debug(TAG, "Found device: " + device.getName() + "::" + device.getAddress());
                        RemoteDeviceMetaData remoteDeviceMetaData = new RemoteDeviceMetaData();
                        remoteDeviceMetaData.setMacAddress(device.getAddress());
                        remoteDeviceMetaData.setName(device.getName());
                        remoteDeviceMetaData.setBluetoothClass(device.getBluetoothClass());
                        remoteDeviceMetaData.setBonded(device.getBondState() == 12);
                        remoteDeviceMetaData.setRssi(intent.getShortExtra("android.bluetooth.device.extra.RSSI", Short.MIN_VALUE));
                        if (VERSION.SDK_INT < 18 || device.getType() != 2) {
                            onDeviceFound(remoteDeviceMetaData);
                        } else {
                            Trace.debug(TAG, "Device is BT-LE; ignoring");
                        }
                    }
                } else if (action.equals("android.bluetooth.adapter.action.DISCOVERY_FINISHED")) {
                    Trace.debug(TAG, "Discovery finished.");
                    onDiscoveryFinished();
                }
            }
        }
    }

    public void stopReceiving() {
        Trace.verbose(TAG, "stopReceiving");
        Context context = (Context) this.mContext.get();
        if (context != null) {
            context.unregisterReceiver(this);
        }
    }
}
